package com.jsb.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.annotation.SystemControllerLog;
import com.jsb.service.CmsArticleService;
import com.jsb.service.CmsCategoryService;
import com.jsb.service.ExtraCategoryService;
import com.jsb.service.UserService;
import com.jsb.common.config.Global;
import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.ArticleCategory;
import com.jsb.dto.TreeCategory;
import com.jsb.model.CmsArticle;
import com.jsb.model.CmsArticleExample;
import com.jsb.model.CmsArticleExample.Criteria;
import com.jsb.model.CmsCategory;
import com.jsb.model.User;
import com.jsb.model.UserExample;

@Controller
public class CmsArticleController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/cmsArticle/list.do";
	
	@Resource
	private CmsArticleService cmsArticleService;
	
	@Resource
	private CmsCategoryService cmsCategoryService;
	
	@Resource
	private ExtraCategoryService extraCategoryService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("/cmsArticle/list")
	//此处为记录AOP拦截Controller记录用户操作  
//	@SystemControllerLog(description = "查看文章列表")
	public String list(
			Model model,String id,String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		getList(model, id,uuid, pageNum, pageSize);
		return "/CmsArticle/list";
//		return "/CmsArticle/treelist";
		
	}

	private void getList(Model model, String id,String uuid, Integer pageNum,
			Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		//栏目id为1下的文章
//		if(StringUtils.isEmpty(id)){
//			id="1";
//		}
		CmsArticleExample cmsArticleExample = new CmsArticleExample();
		Criteria createCriteria = cmsArticleExample.createCriteria();
		cmsArticleExample.setOrderByClause("createDate DESC");
		if(StringUtils.isNotEmpty(id)){
			createCriteria.andCategoryIdEqualTo(id);
		}
		if(StringUtils.isNotEmpty(uuid)){
			createCriteria.andIdEqualTo(uuid);
		}
		int totalCount = cmsArticleService.countCmsArticle(cmsArticleExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		cmsArticleExample.setPageIndex((pageNum - 1) * pageSize);
		cmsArticleExample.setPageSize(pageSize);
		List<CmsArticle> cmsArticles = cmsArticleService.getPageCmsArticle(cmsArticleExample);
		List<ArticleCategory> articleCategories = new ArrayList<ArticleCategory>();
		for (CmsArticle cmsArticle : cmsArticles) {
			String creatBy = cmsArticle.getCreatBy();
			if(StringUtils.isNotEmpty(creatBy)){
				User user = userService.getUserByPhoneNumber(creatBy);
				if(null != user){
					String realName = user.getRealName();
					if(StringUtils.isNotEmpty(realName)){
						cmsArticle.setCreatBy(realName);
					}
				}
			}
			CmsCategory cmsCategoryById = cmsCategoryService.getCmsCategoryById(cmsArticle.getCategoryId());
			ArticleCategory articleCategory = new ArticleCategory();
			articleCategory.setArticle(cmsArticle);
			articleCategory.setCategory(cmsCategoryById);
			articleCategories.add(articleCategory);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("articleCategories", articleCategories);
	}
	
	@RequestMapping("/cmsArticle/treeTable")
	public String treeTable(Model model,String id,String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize){
		getList(model, id,uuid, pageNum, pageSize);
		return "/CmsArticle/treeTable";
	}
	
	@RequestMapping("/cmsArticle/toAdd")
	public String toAdd(Model model
						//@ModelAttribute("cmsCategory") CmsCategory category
						) {
//		CmsCategory cmsCategory = null;
//		if(null != category && null != category.getId()){
//			cmsCategory = cmsCategoryService.getCmsCategoryById(category.getId());
//		}
//		model.addAttribute("cmsCategory",cmsCategory);
		List<TreeCategory> cmsCategories = extraCategoryService.findList(Global.getConfig("root.id"));
		model.addAttribute("cmsCategories",cmsCategories);
		return "/CmsArticle/add";
	}

	@RequestMapping("/cmsArticle/add")
	@SystemControllerLog(description = "添加文章")
	public String add(Model model, @ModelAttribute("cmsArticle") CmsArticle cmsArticle) {
		if(null != cmsArticle && StringUtils.isNotEmpty(cmsArticle.getCategoryId())){
			cmsArticle.setId(IdGen.uuid());
			cmsArticle.setCreateDate(new Date());
			cmsArticleService.addCmsArticle(cmsArticle);
		}
		return redirect;
	}

	@RequestMapping("/cmsArticle/del")
	@SystemControllerLog(description = "删除文章列表")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		cmsArticleService.delCmsArticleById(id);
		return redirect;
	}

	@RequestMapping("/cmsArticle/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		CmsArticle cmsArticle = cmsArticleService.getCmsArticleById(id);
		String creatBy = cmsArticle.getCreatBy();
		UserExample example = new UserExample();
		com.jsb.model.UserExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andPhoneNumberEqualTo(creatBy);
		List<User> users = userService.selectByExample(example );
		if(null != users && users.size()==1){
			String phone = users.get(0).getPhone();
			cmsArticle.setCreatBy(phone);
		}
		model.addAttribute("cmsArticle", cmsArticle);
		
		List<TreeCategory> cmsCategories = extraCategoryService.findList(Global.getConfig("root.id"));
		model.addAttribute("cmsCategories",cmsCategories);
		return "/CmsArticle/edit";
	}

	@RequestMapping("/cmsArticle/edit")
	@RequiresRoles(value = { "customer" })
	@SystemControllerLog(description = "编辑文章")
	public String edit(Model model, @ModelAttribute("cmsArticle") CmsArticle cmsArticle) {
		cmsArticleService.updateCmsArticle(cmsArticle);
		return redirect;
	}

	@RequestMapping("/cmsArticle/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		CmsArticle cmsArticle = cmsArticleService.getCmsArticleById(id);
		model.addAttribute("cmsArticle", cmsArticle);
//		return "/CmsArticle/view";
		return "/CmsArticle/preview";
	}
	/**
	 * 红包打赏文章
	 * @param model
	 * @param questionUuid
	 * @return
	 */
	@RequestMapping("/cmsArticle/viewDetail")
	public String viewDetail(Model model,
			@RequestParam(value = "questionUuid", required = true) String questionUuid) {
		CmsArticle cmsArticle = cmsArticleService.getCmsArticleById(questionUuid);
		model.addAttribute("cmsArticle", cmsArticle);
		return "/CmsArticle/viewDetail";
	}
	
	@RequestMapping("checkPhone")
	@ResponseBody
	public Object checkPhone(String creatBy){
		if(!StringUtils.isMobile(creatBy)){
			return false;
		}
		UserExample example = new UserExample();
		com.jsb.model.UserExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andPhoneEqualTo(creatBy);
		List<User> users = userService.selectByExample(example );
		if(null != users && users.size()==1){
			return true;
		}
//		User userByPhoneNumber = userService.getUserByPhoneNumber(creatBy);
//		if(null == userByPhoneNumber){
//			return false;
//		}
		return false;
	}
}
